<?php
/**
 *
 * Author: chain
 * Date: 2020/10/16 17:26
 */


namespace App\Http\Controllers\System;


use App\Common\Utils\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Request\System\Group\GetAllClassesRequest;
use App\Http\Request\System\Group\GetStudentsByClassIdRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class GroupController extends Controller
{
    /**
     * @var \App\Services\System\GroupService $groupService
     */
    private $groupService;
  
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $this->groupService = app('groupService');
    }

    /**
     * 获取年级列表
     */
    public function getGradesAll()
    {
        $school = Auth::user()->school();
        try
        {
            $result = $this->groupService->getAllGradeCore($school['id'],['id','group_name','parent_id']);
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 获取年级下所有班级
     */
    public function getClassesByGradeId()
    {
        $parent_id = '12895';
        try
        {
            $result = $this->groupService->getAllClasses($parent_id,['id','group_name','parent_id']);
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 选择年级下所有的班级
     * @param GetAllClassesRequest $request
     * @return array
     */
    public function getClassesByGrade(GetAllClassesRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->groupService->getAllClassWithStudentsCore($requestData,['id','group_name']);
                return JsonResponse::get($result);
            }

        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    

    /**
     * 递归获取当前学校下所有数据
     * @author lxy
     * @datetime 2022-11-18
     */
    public function getGroupAllBySchool(Request $Request)
    {
        try{
            $result = $this->groupService->getGroupAllBySchool($Request->all()); 
            return JsonResponse::success($result,'操作成功');
        }catch(\Exception $e){
            return JsonResponse::error($e->getMessage());
        }
    }
    


}
